package com.kaifamiao.list;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;

/**
 * 1、List.of()
 * 2、List.of(E e1 )
 * 3、List.of(E e1 , E e2 , ...... , E e10 )
 * 4、List.of(E... elements)
 * 5、List.copyOf( Collection<? extends E> )
 * 6、List#sort( Comparator<? super E> )
 */
public class ListTest3 {

	public static void main(String[] args) {
		
		List<String> empty = List.of();
		System.out.println( empty );
		System.out.println( empty.getClass() );
		
		List<Integer> single = List.of( 100 ); // 100 : auto-boxing
		System.out.println( single );
		System.out.println( single.getClass() );
		
		List<String> names = List.of( "诸葛亮" , "徐庶" , "司马懿" , "曹操" );
		System.out.println( names );
		System.out.println( names.getClass() );
		
		// names.add( "郭嘉" ); // UnsupportedOperationException
		// names.remove( "司马懿" ); // UnsupportedOperationException
		// names.set( 2 , "司马昭" ); // UnsupportedOperationException
		
		String[] array = { "李云龙" , "楚云飞" , "魏大勇" , "赵刚" };
		List<String> list = List.of( array ); // List.of(E...elements)
		System.out.println( list );
		
		List<String> another = List.copyOf( list );
		System.out.println( another );
		System.out.println( another == list );
		
		Collection<String> collection = new ArrayList<>();
		collection.add( "张大彪" );
		collection.add( "孙德胜" );
		collection.add( "王大柱" );
		
		another = List.copyOf( collection );
		System.out.println( another );
		System.out.println( another == list );
		
		List<Integer> numbers = new ArrayList<>();
		numbers.addAll( List.of( 1 , 2 , 3 , 4 , 5 , 6 , 7 ) );
		System.out.println( numbers );
		// sort 是 List 接口中定义的 default 修饰的 实例方法
		numbers.sort( new Comparator<Integer>(){
			@Override
			public int compare(Integer first, Integer second) {
				return -( first - second ) ;
			}
		});
		System.out.println( numbers );
		
	}

}
